﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace SernaJson
{
    public static class Extenstions    
    {



        //static StringBuilder sb = new StringBuilder();
        static SerializationSettings DefaultSerializationSettings = new SerializationSettings();
        

        public static string SerializeToJsonString(this object obj)
        {
            var sb = new StringBuilder();
            //sb.Clear();
            SernaJson.Serializers.InternalJSONSerializer.SerializeObject(obj, DefaultSerializationSettings, sb);
            return sb.ToString();
            //return InternalJSONSerializer.SerializeObject(obj, new SerializationSettings());
            //InternalJSONSerializer intSer = new InternalJSONSerializer();
            //return intSer.Serialize(obj);
        }


        /// <summary>
        /// Can read
        /// <para>{"p1":"113028906","p2": 12.485,"p3":{"ip1":34,"ip2":"dfs"},"p4":[{"ip1":33,"ip2":"fff"},"sdfs","dsfd"]}</para>
        /// <para>var re = t.DeserializeFromJsonStringToDictionary();</para>
        /// <para>var x1 = re["p4"].DeserializeFromJsonStringToDictionary();</para>
        /// <para>var x2 = x1.First().Key.DeserializeFromJsonStringToDictionary();</para>
        /// <para>when first is [-always array; {-new object</para>
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public static Dictionary<string, string> DeserializeFromJsonStringToDictionary(this string str)
        {
            Serializers.InternalJsonDeserializerToDictionary id = new Serializers.InternalJsonDeserializerToDictionary();
            return id.Deserialize(str);

        }



        /// <summary>
        /// Deserializes object from Serna-String to object T
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="str"></param>
        /// <returns></returns>
        public static T DeserializeFromJsonString<T>(this string str)
        {
            SernaJson.Serializers.InternalJsonDeserializer des = new SernaJson.Serializers.InternalJsonDeserializer();
            return (T)des.Deserialize(typeof(T), str);
            
        }


    }
}
